package com.tengju.bff.interfaces.app.activity;

import com.tengju.activity.application.ActivityService;
import com.tengju.activity.domain.model.activity.ActivityId;
import com.tengju.bff.interfaces.app.activity.facade.ActivityFacade;
import com.tengju.bff.interfaces.app.activity.vo.ActivitySignUpForm;
import com.tengju.bff.interfaces.app.activity.vo.SaleSituation66VO;
import com.tengju.bff.interfaces.shared.ApiResponse;
import com.tengju.bff.interfaces.shared.servlet.ServletContextUtil;
import com.tengju.bff.interfaces.shared.servlet.UserServletContext;
import com.tengju.user.domain.model.user.UserIdCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@Validated
@Slf4j
@RestController
@Api(tags = "app活动")
@RequestMapping(value = "/activity")
public class ActivityController {

    @Autowired
    private ActivityService activityService;

    @Autowired
    private ActivityFacade activityFacade;

    @ApiOperation(value = "活动报名", notes = "活动报名")
    @RequestMapping(value = "signUp", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Boolean> signUp(@Valid @RequestBody ActivitySignUpForm form) {
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        UserIdCode userIdCode = servletContext.needIdCode();
        return ApiResponse.newSuccess(activityService.signUp(
                new com.tengju.activity.domain.model.user.UserIdCode(userIdCode.getValue()),
                new ActivityId(form.getActivityId())));
    }

    @ApiOperation(value = "报名人数", notes = "报名人数")
    @RequestMapping(value = "signUpUserNum", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<Integer> signUpUserNum(@RequestParam @NotNull(message = "活动id不能为空") Long activityId) {
        return ApiResponse.newSuccess(activityService.signUpUserNum(new ActivityId(activityId)));
    }

    @ApiOperation(value = "2021-66销售情况", notes = "66销售情况")
    @RequestMapping(value = "2021/66/saleSituation", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ApiResponse<SaleSituation66VO> saleSituation66() {
        UserServletContext servletContext = ServletContextUtil.getServletContext();
        UserIdCode userIdCode = servletContext.needIdCode();
        return ApiResponse.newSuccess(activityFacade.saleSituation66(userIdCode));
    }

}
